<?php
/**
* @file $Id: ReportCardCodes.php 437 2007-04-23 00:57:51Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['tables'] && $_POST['tables'] && AllowEdit())
{
	foreach($_REQUEST['tables'] as $table=>$values)
	{
		foreach($values as $id=>$columns)
		{
			if($columns['DEFAULT_BREAKOFF'])
				$columns['DEFAULT_BREAKOFF'] = ereg_replace('[^0-9]+','',$columns['DEFAULT_BREAKOFF']);
			if($id!='new')
			{
				$sql = "UPDATE $table SET ";
								
				foreach($columns as $column=>$value)
				{
					$sql .= $column."='".str_replace("\'","''",$value)."',";
				}
				$sql = substr($sql,0,-1) . " WHERE ID='$id'";
				DBQuery($sql);
			}
			else
			{
				$sql = "INSERT INTO $table ";
	
				$fields = 'ID,SCHOOL_ID,SYEAR,SCALE_ID,';
				$values = db_seq_nextval($table.'_SEQ').",'".UserSchool()."','".UserSyear()."','".$_REQUEST['scale_id']."',";
	
				$go = 0;
				foreach($columns as $column=>$value)
				{
					if($value || $value==='0')
					{
						$fields .= $column.',';
						$values .= "'".str_replace("\'","''",$value)."',";
						
						if($column!='CREDITS')
							$go = true;
					}
				}
				$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
				
				if($go)
					DBQuery($sql);
			}
		}
	}
}

DrawHeader(ProgramTitle());

if($_REQUEST['new_category_title'])
{
	$id = DBGet(DBQuery("SELECT ".db_seq_nextval('REPORT_CARD_GRADE_SCALES_SEQ').' AS ID'.FROM_DUAL));
	$id = $id[1]['ID'];

	DBQuery("INSERT INTO REPORT_CARD_GRADE_SCALES (SYEAR,SCHOOL_ID,ID,TITLE) values('".UserSyear()."','".UserSchool()."',".$id.",'".$_REQUEST['new_category_title']."')");
	$_REQUEST['scale_id'] = $id;
}

if($_REQUEST['modfunc']=='remove')
{
	if($_REQUEST['table']=='REPORT_CARD_GRADES')
		$code_type = _('report card grade');
	elseif($_REQUEST['table']=='REPORT_CARD_COMMENTS')
		$code_type = _('report card comment');
	elseif($_REQUEST['table']=='REPORT_CARD_GRADE_SCALES')
		$code_type = _('grading scale');
	
	if($_REQUEST['table']=='REPORT_CARD_GRADE_SCALES')
	{
		if(DeletePrompt($code_type))
		{
			DBQuery("DELETE FROM REPORT_CARD_GRADE_SCALES WHERE ID='".$_REQUEST['scale_id']."'");
			unset($_REQUEST['scale_id']);
			unset($_REQUEST['modfunc']);
		}
	}
	else
	{
		if(DeletePrompt($code_type))
		{
			DBQuery("DELETE FROM ".$_REQUEST['table']." WHERE ID='".$_REQUEST['id']."'");
			unset($_REQUEST['modfunc']);
		}
	}
}

if(!isset($_REQUEST['scale_id']))
{
	$scales_RET = DBGet(DBQuery("SELECT ID FROM REPORT_CARD_GRADE_SCALES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY COALESCE(DEFAULT_SCALE,'N') DESC"));
	if(count($scales_RET))
		$_REQUEST['scale_id'] = $scales_RET[1]['ID'];
	else
		$_REQUEST['scale_id'] = 'new';
}

if($_REQUEST['modfunc']!='remove')
	echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=update&scale_id=$_REQUEST[scale_id] method=POST>";

if($_REQUEST['modfunc']!='remove')
{
	DrawHeader('',SubmitButton(_('Save')));
	echo '<BR>';
	echo '<TABLE width=100%><TR><TD width=50% align='.ALIGN_LEFT.' valign=top>';
	
	DrawHeader(_('Report Card Grades'));
	echo '<BR>';
	if($_REQUEST['scale_id']!=='new')
	{
		$sql = "SELECT ID,TITLE,SORT_ORDER,GPA_VALUE,CREDITS,DEFAULT_BREAKOFF FROM REPORT_CARD_GRADES WHERE SCALE_ID='".$_REQUEST['scale_id']."' AND SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER";
		$QI = DBQuery($sql);
		$RET = DBGet($QI,array('TITLE'=>'_makeGradesInput','SORT_ORDER'=>'_makeGradesInput','GPA_VALUE'=>'_makeGradesInput','CREDITS'=>_('_makeCheckbox'),'DEFAULT_BREAKOFF'=>'_makeGradesInput'));
	}

	$columns = array('TITLE'=>_('Title'),'GPA_VALUE'=>_('GPA Value'),'SORT_ORDER'=>_('Sort Order'),'CREDITS'=>_('Gives Credit'),'DEFAULT_BREAKOFF'=>_('Default Breakoff'));
	$link['add']['html'] = array('TITLE'=>_makeGradesInput('','TITLE'),'GPA_VALUE'=>_makeGradesInput('','GPA_VALUE'),'CREDITS'=>_makeCheckbox('','CREDITS'),'SORT_ORDER'=>_makeGradesInput('','SORT_ORDER'),'DEFAULT_BREAKOFF'=>_makeGradesInput('','DEFAULT_BREAKOFF'));
	$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove&table=REPORT_CARD_GRADES&scale_id=".$_REQUEST['scale_id'];
	$link['remove']['variables'] = array('id'=>'ID');
	
	$categories_RET = DBGet(DBQuery("SELECT ID,TITLE FROM REPORT_CARD_GRADE_SCALES WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	foreach($categories_RET as $category)
		$tabs[] = array('title'=>$category['TITLE'],'link'=>"Modules.php?modname=$_REQUEST[modname]&scale_id=".$category['ID']);

	if($_REQUEST['scale_id']==='new')
		$tabs[] = array('title'=>button('white_add'),'link'=>"Modules.php?modname=$_REQUEST[modname]&scale_id=new");
	else
		$tabs[] = array('title'=>button('add'),'link'=>"Modules.php?modname=$_REQUEST[modname]&scale_id=new");

	if($_REQUEST['scale_id']!=='new')
	{
		if(count($RET)==0)
		{
			$_FOCUS['selected_tab'] = "Modules.php?modname=$_REQUEST[modname]&scale_id=$_REQUEST[scale_id]";
			echo PopTable('header',$tabs);
			ListOutput($RET,$columns,_('Report Card Grades'),_('Report Card Grades'),$link,array(),array('download'=>false,'search'=>false));
			if($_REQUEST['scale_id']!=0)
				echo '<CENTER>'.button('remove',_('Delete this grading scale'),"Modules.php?modname=$_REQUEST[modname]&modfunc=remove&scale_id=$_REQUEST[scale_id]&table=REPORT_CARD_GRADE_SCALES");
			echo '<BR><CENTER>'.SubmitButton(_('Save')).'</CENTER>';
			echo PopTable('footer');
		}
		else
		{
			echo '<CENTER>'.WrapTabs($tabs,"Modules.php?modname=$_REQUEST[modname]&scale_id=$_REQUEST[scale_id]").'</CENTER>';
			ListOutput($RET,$columns,'','',$link,array(),array('download'=>false,'search'=>false));
			echo '<BR><CENTER>'.SubmitButton(_('Save')).'</CENTER>';
		}
	}
	else
	{
		$_FOCUS['selected_tab'] = "Modules.php?modname=$_REQUEST[modname]&scale_id=$_REQUEST[scale_id]";
		echo PopTable('header',$tabs);
		echo '<CENTER>'._('New Grading Scale Title').' <INPUT type=text name=new_category_title></CENTER>';
		echo '<BR><CENTER>'.SubmitButton(_('Save')).'</CENTER>';
		echo PopTable('footer');
	}
	
	echo '</TD><TD width=50% align='.ALIGN_LEFT.' valign=top>';
	
	DrawHeader(_('Report Card Comments'));
	
	$sql = "SELECT ID,TITLE FROM REPORT_CARD_COMMENTS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY TITLE";
	$QI = DBQuery($sql);
	$RET = DBGet($QI,array('TITLE'=>'_makeCommentsInput'));
	
	$columns = array('TITLE'=>_('Title'));
	$link['add']['html'] = array('TITLE'=>_makeCommentsInput('','TITLE'));
	$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove&table=REPORT_CARD_COMMENTS";
	$link['remove']['variables'] = array('id'=>'ID');
	
	ListOutput($RET,$columns,_('Report Card Comment'),_('Report Card Comments'),$link,array(),array('center'=>false));
	
	echo '<CENTER>'.SubmitButton(_('Save')).'</CENTER></TD></TR></TABLE>';
	echo '</FORM>';
}

function _makeGradesInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';
	
	if($name=='DEFAULT_BREAKOFF')
	{
		$extra = ' size=3 maxlength=3';
		$suffix = '%';
	}
	elseif($name!='GPA_VALUE')
		$extra = 'size=5 maxlength=3';
	else
		$extra = 'size=5 maxlength=5';
	
	return TextInput(array($value,$value.$suffix),'tables[REPORT_CARD_GRADES]['.$id.']['.$name.']','',$extra);
}

function _makeCommentsInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';
	
	return TextInput($value,'tables[REPORT_CARD_COMMENTS]['.$id.']['.$name.']','',$extra);
}

function _makeCheckbox($value,$column)
{	global $THIS_RET;

	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
	{
		$id = 'new';
		$value = 'Y';
		$new = true;
	}

	return CheckboxInput($value,'tables[REPORT_CARD_GRADES]['.$id.']['.$column.']','','',$new);
}

?>